from http import HttpError

class GameBase(object):
    def __init__(self, config):
        self.gamecounter = 1
        self.games = {
                666: {
                    'players': ['foo', 'bar'],
                    'info': {'width': 9, 'height': 9, 'pieces': 6},
                    'turns': []
                    },
                667: {
                    'players': ['foo', 'zoo'],
                    'info': {'width': 4, 'height': 4, 'pieces': 2},
                    'turns': []
                    },
                668: {
                    'players': ['quux', ''],
                    'info': {'width': 13, 'height': 13, 'pieces': 8},
                    'turns': []
                }
            } 
        
    def addgame(self, username, color, info):
        if color == 'red': #TODO: players
            players = [username, '']
        else:
            players = ['', username]

        self.games[self.gamecounter] = {'players': players,
                                      'info': info,
                                      'turns': []
                                       }
        self.gamecounter += 1
        return self.gamecounter-1

    def joingame(self, username, gameid):
        if '' in self.games[gameid]['players']:
            if username not in self.games[gameid]['players']:
                self.games[gameid]['players'][self.games[gameid]['players'].index('')] = username
                return gameid
        return False

    def abandongame(self, username, gameid):
        if gameid in self.games and username in self.games[gameid]['players']:
            self.games.pop(gameid)
            return gameid
        return False

    def turn(self, player, gameid, turn, newstate):
        # TODO: what if game has been abandoned?
        if gameid not in self.games: raise HttpError(403, content='cannot make turn: game does not exist')
        
        game = self.games[gameid]
        currentTurn = len(game['turns'])
        if turn != currentTurn + 1: return False

        currentSide = currentTurn % 2
        currentPlayer = game['players'][currentSide]
        if player != currentPlayer: return False
        
        # TODO: Check that the given move is a valid move
        # validate_move(game['turns'][-1], newstate)
        
        game['turns'].append(newstate)
        
        return True
        
    def getturns(self, gameid, turn):
        # TODO: what if game has been abandoned?
        if gameid not in self.games: raise HttpError(403, content='cannot read turns: game does not exist')
        game = self.games[gameid]
        allturns = game['turns']
        return {
            'gameid': gameid,
            'info': game['info'],
            'players': game['players'],
            'turn': len(allturns),
            'turns': allturns[turn:]
            }
        
    def getgames(self):
        return [self.getturns(gameid, -1) for gameid in self.games.iterkeys()]

    def getmygames(self, player):
        return [self.getturns(gameid, -1) for gameid in [gameid for gameid, game in self.games.iteritems() if player in game.get('players')]]
